<?php
    include('simple_html_dom.php');
    include('conexion.php');
    db_connect();

    function extrae_numeros($cad) {
        $arr = str_split($cad);
        $n = count($arr);
        $j = 0;
        $arr_str[$j] = "";
        for ($i = 0; $i < $n; $i++) {
            if (is_numeric($arr[$i])) {
                $arr_str[$j].=$arr[$i];
            } else {
                if ($i > 0 && is_numeric($arr[$i - 1])) {
                    $j++;
                    $arr_str[$j] = "";
                }
            }
        }
        return $arr_str;
    }
?>

<?php
    require ('estructura/header.php');
?>
        <body onload="showDivArea()">
        <form action="index.php" method="POST">
                Website
                <select name="pagina" id="pagina" onchange="cargarLink('div_url'), showDivArea()">
                    <option value='1' <?php if(isset($_POST['pagina'])){if($_POST['pagina'] == 1){ echo 'selected';} } ?> >
                        Bumeran
                    </option>
                    <option value='2' <?php if(isset($_POST['pagina'])){if($_POST['pagina'] == 2){ echo 'selected';} } ?> >
                        Aptitus
                    </option>
                    <option value='3' <?php if(isset($_POST['pagina'])){if($_POST['pagina'] == 3){ echo 'selected';} } ?> >
                        CompuTrabajo
                    </option>
                </select><br/>
                <div id="div_url">
                URL
                <input type="text" name="url" id="url" size="200" maxlength="200" value="<?php if(isset($_POST['url'])){ echo $_POST['url'];} else echo 'http://www.bumeran.com.pe/empleos-publicacion-hoy.html'; ?>"><br/>
                </div>
                Criterio
                <input type="text" name="criterio" id="criterio" size="100" maxlength="100" value="<?php if(isset($_POST['criterio'])){ echo $_POST['criterio'];} ?>"><br/> 
                <div id="div_cp_area">
                CompuTrabajo - Area
                <select name="idarea" id="idarea" >
                    <!-- <option value="0"> Seleccione</option> -->
                    <?php
                    $area = null;
                    if(isset($_POST['idarea'])){$area = $_POST['idarea'];}                    
                    $query = "SELECT * FROM computrabajoarea";
                    $result = mysql_query($query);
                    $num = mysql_numrows($result);
                    $i = 0;
                    while ($i < $num) {
                        $id_Area = mysql_result($result, $i, "area_idarea");
                        $nombreArea = utf8_encode( mysql_result($result, $i, "nombre"));
                            if ($id_Area == $area) {
                            ?>
                            <option selected="true" value=<?php echo "\"$id_Area\""; ?>>
                                <?php echo "BSC00".$i." - ".$nombreArea ?>
                            </option>
                            <?php
                            $i++;
                        } else {
                            ?>
                            <option value=<?php echo "\"$id_Area\""; ?>>
                                <?php echo "BSC00".$i." - ".$nombreArea ?>
                            </option>
                            <?php
                            $i++;
                        }
                    }
                    ?>
                </select>
                </div>
                
                
                <input type="submit" value="Buscar"/>
        </form>
        
        <?php

        if (isset($_POST['url']) && !isset($_POST['nro'])) {
            $url = $_POST['url'];
            if($_POST['pagina'] == 2){
                $url = str_replace('/buscar/','/search-cmp/busqueda/',$url);
            }
            $html = file_get_html($url);
            $pag = $_POST['pagina'];

            switch ($pag) {
                case 1:
                    ?><p>Hay <?php
                    $var = $html->find('.letra-chica');
                    echo $var[0]->plaintext;
//                  extrae_numeros($html->find('.letra-chica')->plaintext); 
                    ?></p>                   
                    <?php
                    break;
                
                case 2:
                    $leg = $html->find('p.pagLegend');
                    $arr_str = extrae_numeros($leg[0]->plaintext);
                    echo "Hay " . $arr_str[1] . " avisos de trabajo encontrados...<br>";
                    break;
                
                case 3:
                    $cant_anun = $html->find('table[cellpadding=2] tr td[align=center] font[size=2]');
                    echo $cant_anun[1]->plaintext;
                    break;
            }
            ?>
            <form action="procesa_anuncio.php" method="POST">
                Cuantos avisos vas a querer ver
                <input type="hidden" name="url" value="<?php if(isset($_POST['url'])){ echo $url;} ?>"/>
                <input type="hidden" name="idareah" value="<?php if(isset($_POST['idarea'])){ echo $_POST['idarea'];} ?>"/>
                <input type="hidden" name="criterio" value="<?php if(isset($_POST['criterio'])){ echo $_POST['criterio'];} ?>"/>
                <input type="hidden" name="pagina" value="<?php if(isset($_POST['pagina'])){ echo $_POST['pagina'];} ?>"/>
                Inicio: <input type="text" name="inicio" value="" />
                Fin: <input type="text" name="fin" value="" />
                <input type="submit" value="Guardar"/>
            </form>
            <?php
        }
        ?>
    </body>
</html>
